#define _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"
//初始化队列
void QueueInit(Queue* ps)
{
	assert(ps);
	ps->tail = NULL;
	ps->head = NULL;
	ps->size = 0;
}

//进队列
void QueuePush(Queue* ps, DataType x)
{
	assert(ps);
	Qnode* newnode = (Qnode*)malloc(sizeof(Qnode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	newnode->val = x;
	newnode->next = NULL;

	//判断是否是第一次进队列(尾插)
	if (ps->tail == NULL)
	{
		ps->head = ps->tail = newnode;
	}
	else
	{
		ps->tail->next = newnode;
		ps->tail = newnode;
	}
	ps->size++;
}
//出队列(头删)
void QueuePop(Queue* ps)
{
	assert(ps);
	//检查队列是否为空
	assert(!QueueEmpty(ps));
	//只有一个元素  head和tail在同一节点
	if (ps->head->next == NULL)
	{
		free(ps->head);
		ps->tail = ps->head = NULL;
	}
	else
	{
		Qnode* next = ps->head->next;
		free(ps->head);
		ps->head = next;
	}

	ps->size--;
}
//销毁队列
void QueueDestory(Queue* ps)
{
	assert(ps);

	Qnode* cur = ps->head;
	while (cur != NULL)
	{
		Qnode* next = cur->next;
		free(cur);
		cur = next;
	}

	ps->head = ps->tail = NULL;
	ps->size = 0;
}
//获取队列头元素
DataType QueueFront(Queue* ps)
{
	assert(ps);
	//检查队列是否为空
	assert(!QueueEmpty(ps));
	return ps->head->val;
}
//获取队列尾元素
DataType QueueBack(Queue* ps)
{
	assert(ps);
	//检查队列是否为空
	assert(!QueueEmpty(ps));
	return ps->tail->val;
}
//检测队列是否为空
bool QueueEmpty(Queue* ps)
{
	assert(ps);
	return ps->head == NULL;
}

int QueueSzie(Queue* ps)
{
	assert(ps);
	return ps->size;
}